using System;
using System.IO;
using System.Linq;

class Program
{
    // 定义有效阶段集合
    private static readonly string[] ValidPeriods = { "stage1", "stage2", "FF1", "FF2", "FF3" };

    static void Main(string[] args)
    {
        // 1. 读取文件夹路径
        var sections = ReadInput(
            prompt: "请输入文件夹路径（Windows格式）: ",
            validator: ValidateFolderPath);

        // 2. 读取文件路径
        var details = ReadInput(
            prompt: "请输入文件路径（全路径）: ",
            validator: ValidateFilePath);

        // 3. 读取阶段信息
        var period = ReadInput(
            prompt: "请输入阶段（stage1/stage2/FF1/FF2/FF3）: ",
            validator: ValidatePeriod);

        // 4. 读取年份
        var year = ReadInput(
            prompt: "请输入四位数的年份: ",
            validator: ValidateYear);

        // 5. 读取生产文件路径
        var productions = ReadInput(
            prompt: "请输入生产文件路径（全路径）: ",
            validator: ValidateFilePath);

        // 6. 输出所有信息
        Console.WriteLine("\n输入的信息如下：");
        Console.WriteLine($"1. 文件夹路径: {sections}");
        Console.WriteLine($"2. 文件路径: {details}");
        Console.WriteLine($"3. 阶段: {period}");
        Console.WriteLine($"4. 年份: {year}");
        Console.WriteLine($"5. 生产文件路径: {productions}");

        Console.WriteLine("\n按任意键退出...");
        Console.ReadKey();
    }

    /// <summary>
    /// 通用输入验证方法
    /// </summary>
    static T ReadInput<T>(string prompt, Func<string, (bool IsValid, T Value)> validator)
    {
        while (true)
        {
            Console.Write(prompt);
            var input = Console.ReadLine();
            var result = validator(input);

            if (result.IsValid)
            {
                return result.Value;
            }

            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine("输入无效，请重新输入。");
            Console.ResetColor();
        }
    }

    /// <summary>
    /// 验证文件夹路径
    /// </summary>
    static (bool IsValid, string Value) ValidateFolderPath(string input)
    {
        try
        {
            if (Directory.Exists(input) &&
                Path.IsPathRooted(input) &&
                !string.IsNullOrWhiteSpace(input))
            {
                return (true, input);
            }
        }
        catch (Exception ex) when (
            ex is ArgumentException ||    // 包含非法字符
            ex is PathTooLongException || // 路径过长
            ex is NotSupportedException)  // 包含冒号等无效字符
        {
        }
        return (false, null);
    }

    /// <summary>
    /// 验证文件路径
    /// </summary>
    static (bool IsValid, string Value) ValidateFilePath(string input)
    {
        try
        {
            if (File.Exists(input) &&
                Path.IsPathRooted(input) &&
                !string.IsNullOrWhiteSpace(input))
            {
                return (true, input);
            }
        }
        catch (Exception ex) when (
            ex is ArgumentException ||
            ex is PathTooLongException ||
            ex is NotSupportedException)
        {
        }
        return (false, null);
    }

    /// <summary>
    /// 验证阶段信息
    /// </summary>
    static (bool IsValid, string Value) ValidatePeriod(string input)
    {
        return ValidPeriods.Contains(input)
            ? (true, input)
            : (false, null);
    }

    /// <summary>
    /// 验证年份
    /// </summary>
    static (bool IsValid, int Value) ValidateYear(string input)
    {
        if (input.Length != 4 ||
            !input.All(char.IsDigit) ||
            !int.TryParse(input, out int year) ||
            year < 1900 ||
            year > DateTime.Now.Year + 5)
        {
            return (false, 0);
        }
        return (true, year);
    }
}